﻿#include "qfish.h"
#include<QDebug>
QFish::QFish(const QString &fileName,QGraphicsScene *scene):QPixItem(fileName,  scene)
{
    setPos(-100,200+qrand()%400);
}

void QFish::advance(int phase)
{
    //返回true可视
    if(mapToScene(0,0).y()<=0||mapToScene(0,0).x()>1295){
        setPos(-100,200+qrand()%400);
    }
    int speed=qrand()%5+5;
    //场景中的x坐标每次加5(x坐标相对于scene)
    this->setPos(mapToScene(speed,-2));
}

void QFish::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    static int i=1;
    if(i==5)
        i=1;
     QString str=QString(":/image/shark_r%1.png").arg(i++);
    pix.load(str);
    painter->drawPixmap(-pix.width()/2,-pix.height(),pix);

}

void QFish::fish_death()
{
    setPos(-100,200+qrand()%400);
}
